package com.hylz.nbs.filter;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hylz.nbs.util.ConstantKey;
/**
 * 后台权限校验
 * @author H
 * @date 2021年7月18日
 */
@WebFilter("/back/manager/*")
public class BackPermissionVerificationFilter implements Filter{

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) resp;
		if(request.getSession().getAttribute(ConstantKey.CURRENTLOGINADMIN) == null) {
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out = response.getWriter();
			String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
			out.print("<script>alert('请先登录..');location.href='" + basePath + "/back/index.html'</script>");
			out.flush();
			out.close();
			return;
		}
		chain.doFilter(request, response);
	}

}
